const express = require("express");
const app = express();
const port = 3000; // 服务端口


const userRouter=require('./router/user.js')
const courseRouter=require('./router/course.js')
const coachRouter=require('./router/coach.js')
const activityRouter=require('./router/activity.js')
const indexRouter=require('./router/index.js')
const shareRouter=require('./router/share.js')
const commentRouter=require('./router/comment.js')
const planRouter=require('./router/plan.js')
const searchRouter=require('./router/search.js')

//静态资源
app.use(express.static('./upload'));
// 配置跨域
const cors = require("cors");
app.use(
  cors({
    origin: "*",
  })
);

// 解析post请求参数
app.use(express.urlencoded());

// 挂载路由
app.use('/v1/user',userRouter);
app.use('/v1/course',courseRouter);
app.use('/v1/coach',coachRouter);
app.use('/v1/activity',activityRouter)
app.use('/v1/index',indexRouter)
app.use('/v1/share',shareRouter)
app.use('/v1/comment',commentRouter)
app.use('/v1/plan',planRouter)
app.use('/v1/search',searchRouter)

// 处理404中间件
app.use(function(req, res, next) {
  res.send({code:404, msg:'资源不存在'})
});

// 错误处理中间件
app.use(function(err, req, res, next) {
  console.log(err)
	res.send({code: 500, msg: '服务器端错误'})
});


app.listen(port, () => {
  console.log("后端服务已启动...");
});